"""
银行相关接口
"""

from datetime import datetime
from db import db_handler
from conf import settings
from lib import common


logger = common.get_logger("bank")


# 充值接口
def recharge_interface(login_user, recharge_amount):

    # 获取用户的数据
    user_data = db_handler.select_data(login_user)

    # 判断用户是否存在
    if not user_data:
        return True, f"用户{login_user}不存在，充值失败。"

    # 给user_data加上充值金额
    user_data["balance"] += recharge_amount

    # 在保存数据之前，记录流水信息
    msg = f"{datetime.now().strftime('%Y-%m-%d %H:%M')} {login_user}充值成功，充值金额为{recharge_amount}元。当前余额为{user_data['balance']}元。"
    user_data["records"].append(msg)

    # 调用数据处理层，保存修改之后的金额
    db_handler.save_data(user_data)

    logger.info(msg)

    return True, msg


# 提现接口
def withdraw_interface(login_user, withdraw_amount):

    # 获取用户的数据
    user_data = db_handler.select_data(login_user)

    # 计算手续费
    service_fee = withdraw_amount * settings.RATE  # 10%手续费

    # 判断用户余额是否足够
    if user_data["balance"] < (withdraw_amount + service_fee):
        return (
            False,
            f"{login_user}提现失败，余额不足。\n当前余额为{user_data['balance']}元。",
        )

    # 给user_data减去提现金额和手续费
    user_data["balance"] -= withdraw_amount + service_fee

    # 在保存数据之前，记录流水信息
    msg = f"{datetime.now().strftime('%Y-%m-%d %H:%M')} {login_user}提现成功，提现金额为{withdraw_amount}元。手续费为{service_fee}元。当前余额为{user_data['balance']}元。"
    user_data["records"].append(msg)

    # 调用数据处理层，保存修改之后的金额
    db_handler.save_data(user_data)

    logger.info(msg)

    return True, msg


# 查询余额接口
def check_balance_interface(login_user):

    # 获取用户的数据
    user_data = db_handler.select_data(login_user)

    logger.info(f"{login_user}查询余额成功。")

    return (
        True,
        f"{login_user}的当前余额为{user_data['balance']}元。",
    )


# 转账接口
def transfer_interface(login_user, transfer_to, transfer_amount):

    # 获取用户的数据
    user_data = db_handler.select_data(login_user)

    # 判断用户余额是否足够
    if user_data["balance"] < transfer_amount:
        return (
            False,
            f"{login_user}转账失败，余额不足。\n当前余额为{user_data['balance']}元。",
        )

    transfer_to_data = db_handler.select_data(transfer_to)

    # 判断转账对象是否存在
    if not transfer_to_data:
        return (
            False,
            f"{transfer_to}不存在，转账失败。",
        )

    # 给transfer_to加上转账金额
    transfer_to_data["balance"] += transfer_amount

    # 给user_data减去转账金额
    user_data["balance"] -= transfer_amount

    # 在保存数据之前，记录流水信息
    msg = f"{datetime.now().strftime('%Y-%m-%d %H:%M')} {login_user}向{transfer_to}转账成功，转账金额为{transfer_amount}元。{transfer_to}的余额为{transfer_to_data['balance']}元。{login_user}的余额为{user_data['balance']}元。"
    user_data["records"].append(msg)
    transfer_to_data["records"].append(msg)

    # 调用数据处理层，保存修改之后的金额
    db_handler.save_data(user_data)
    db_handler.save_data(transfer_to_data)

    logger.info(msg)

    return True, msg


# 查询流水接口
def check_records_interface(login_user):

    # 获取用户的数据
    user_data = db_handler.select_data(login_user)

    # 格式化输出流水信息
    records = user_data["records"]

    logger.info(f"{login_user}查询流水成功。")

    return (
        True,
        records,
    )


# 支付接口
def pay_interface(login_user, total_amount):

    # 获取用户的数据
    user_data = db_handler.select_data(login_user)

    # 判断用户余额是否足够
    if user_data.get("balance", 0) < total_amount:
        return (
            False,
            f"用户{login_user}余额不足，支付失败。\n当前余额为{user_data['balance']}元。",
        )
    # 支付
    user_data["balance"] -= total_amount

    # 在保存数据之前，记录流水信息
    msg = f"{datetime.now().strftime('%Y-%m-%d %H:%M')} {login_user}支付成功，支付金额为{total_amount}元。当前余额为{user_data['balance']}元。"
    user_data["records"].append(msg)

    # 调用数据处理层，保存修改之后的金额
    db_handler.save_data(user_data)

    logger.info(msg)

    return True, msg
